<?php
/**
 * 搜索
 * @link https://www.leadshop.vip/
 * @copyright Copyright ©2020-2021 浙江禾成云计算有限公司
 */

namespace leadmall\api;

use leadmall\Map;
use system\api\RolesController as RolesModules;
use Yii;
use yii\web\ForbiddenHttpException;
use system\models\Roles;


class RolesController extends RolesModules implements Map
{
    public $modelClass = 'system\models\Roles';
    public $modelClass2 = 'system\models\Menus';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['create']);
        unset($actions['index']);
        unset($actions['delete']);
        unset($actions['view']);
        return $actions;
    }

    /**
     * 用户详情
     * @return [type] [description]
     */
    public function actionView()
    {
        $id = Yii::$app->request->get('id', false);

        $modelClass = $this->modelClass;
        $result = $modelClass::find()
            ->where(['id' => $id])
            ->asArray()
            ->one();

        if (empty($result)) {
            Error('角色找不到');
        }
        return $result;
    }

    public function actionIndex()
    {
        $modelClass = $this->modelClass;
        $modelClass2 = $this->modelClass2;
        $rolesClass=$modelClass::find()->where(['is_deleted' => 0])->all();
        foreach ($rolesClass as $keyb => $value) {
            $vidba =[];
            if($value['visits']) {
                $visitArray = json_decode($value['visits'], true);
                $list = $modelClass2::find()->select('id,title,parent_id')->where(['is_deleted' => 0])
                    ->andWhere(['in', 'id', $visitArray])->all();
                foreach ($list as $kbc => $ve) {
                    $vidba[$kbc]['id'] = $ve['id'];
                    $vidba[$kbc]['label'] = $ve['title'];
                    $vidba[$kbc]['parent_id'] = $ve['parent_id'];
                }
                $rolesClass[$keyb]['visits'] = $vidba;
            }else{
                $rolesClass[$keyb]['visits'] = '';
            }
        }
        return $rolesClass;
    }

    public function actionCreate()
    {
        $post = Yii::$app->request->post();
        $post_values = array_values($post);
        $post_keys = array_keys($post);
        $post = array_combine($post_keys, $post_values);
        $time = time();
        $post['visits'] = json_encode($post['visits']);
        if (strlen($post['id'])>0) {
            $model =  Roles::findOne($post['id']);

            if (empty($model)) {
                Error('角色不存在');
            }
            $scenarios = 'update';
            $post['updated_time'] = $time;
        } else {
            $model                =  new Roles();
            $scenarios            = 'create';
            $post['created_time'] = $time;
            $post['updated_time'] = $time;
        }
        $transaction = Yii::$app->db->beginTransaction(); //启动数据库事务
        //统一替换本地文件地址
        $post = url2str($post);

        $model->setScenario($scenarios);
        $model->setAttributes($post);
        if ($model->validate()) {
            $res = $model->save();
            if ($res) {
                $id = $model->id;
                $transaction->commit();
                return ['id' => $id];
            } else {
                $transaction->rollBack();
                Error('保存失败');
            }
        }
        return $model;
    }

    public function actionDelete()
    {
        $get        = Yii::$app->request->get();
        $modelClass = $this->modelClass;
        $id         = intval($get['id']);

        $model      = $modelClass::findOne($id);
        if ($model) {

            $model->deleted_time = time();
            $model->is_deleted   = 1;
            $data = [
                'is_deleted' => 1,
                'deleted_time' => time(),
                'updated_time' => time(),
            ];
            $result =   $modelClass::updateAll($data, ['id' => $id]);
            if ($result) {
                return $result;
                //Yii::$app->getResponse()->setStatusCode(204);
                //return 1;
            } else {
                throw new ForbiddenHttpException('删除失败，请检查is_deleted字段是否存在');
            }
        } else {
            throw new ForbiddenHttpException('删除失败，数据不存在');
        }
    }

}
